package com.powernode.factory;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.powernode.ex.AuthException;
import com.powernode.strategy.LoginStrategy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.stereotype.Component;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * @Author LF
 * projectName:today-shop
 * description:
 * time:2022-09-16 23:13
 */
@Component
public class LoginStrategyFactory {

    @Autowired
    private Map<String, LoginStrategy> loginStrategyHashMap = new ConcurrentHashMap<>();


    public UserDetails realDoLogin(String loginType, String username) {
        LoginStrategy loginStrategy = loginStrategyHashMap.get(loginType);
        if (ObjectUtils.isEmpty(loginStrategy)) {
            throw new AuthException("登录类型不匹配，非法登录");
        }
        return loginStrategy.doLogin(username);
    }

}
